﻿using Corina.Datamodel.DataObject;
using Corina.Enumerations;
using Corina.Win.Helpers;
using Corina.Win.Interface;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;

namespace Corina.Win.ServerHub.Viewmodels
{
    [Export(typeof(IView))]
    public class DeviceMeasureViewmodel : PageViewmodelBase<Corina.Win.ServerHub.App>
    {
        #region Constructor
        public DeviceMeasureViewmodel()
        {
            Title = "测量数据";
            UriString = "DeviceMeasureView";
            UriType = UriType.OutApp;
            Icon = "ICON_MEASURING";
            if (DataContext.IsValid)
            {
                LoadData();
            }
        }

        protected override void LoadData(params object[] paras)
        {
            Task<ResultInfo> task = DeviceMeasureClient.GetAllDeviceMeasureAsync(Account, Password, DeviceSn, 0, 0, TestName, false, Index, 20);
            try
            {
                task.Wait();
            }
            catch(Exception ex)
            {
                ShowPrompt(ex.InnerException.Message);
                ErrorTraceHelper.Trace(ex);
            }

            task.ContinueWith((m) => {
                Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(() =>
                    {
                        if (task.IsCompleted)
                        {
                            if (task.Exception == null)
                            {
                                var items = (from t in task.Result.DataObjects
                                             select new
                                             {
                                                 Id = t["Id"],
                                                 DeviceSn = (string)t["DeviceSn"],
                                                 Kind = (string)t["Kind"],
                                                 ProofValue = t["ProofValue"],
                                                 Name = t["Name"],
                                                 Density = (double)t["Density"],
                                                 Temperature = (double)t["Temperature"],
                                                 Humidity = (double)t["Humidity"],
                                                 Tester = (string)t["Tester"],
                                                 TransType = (TransTypeValue)Convert.ToInt32(t["TransType"]),
                                                 TestTime = Convert.ToDateTime(t["TestTime"]),
                                                 IsOpen = Convert.ToBoolean(t["IsOpen"]),
                                                 UserName = t["UserName"],
                                                 MobileOrIP = t["MobileOrIP"]
                                             });
                                ItemsCount = task.Result.TotalCount;
                                ItemsCollection = new ObservableCollection<object>(items);
                            }
                            else
                            {
                                ErrorTraceHelper.Trace(task.Exception);
                            }
                        }
                    }));
            });
            
            
        }
        #endregion

        #region Collection
        private ObservableCollection<object> _itemsCollection;
        public ObservableCollection<object> ItemsCollection
        {
            get { return _itemsCollection; }
            set
            {
                _itemsCollection = value;
                RaisePropertyChanged(() => ItemsCollection);
            }
        }

        #endregion

        #region Search
        private string _deviceSn = string.Empty;
        public string DeviceSn
        {
            get { return _deviceSn; }
            set
            {
                _deviceSn = value;
                RaisePropertyChanged(() => DeviceSn);
            }
        }

        private string _testName = string.Empty;
        public string TestName
        {
            get { return _testName; }
            set
            {
                _testName = value;
                RaisePropertyChanged(() => TestName);
            }
        }
        #endregion
    }
}
